package cn.hsa.fqz.task.service.impl;

import cn.hsa.fqz.common.enums.DealState;
import cn.hsa.fqz.task.service.dto.ApplyReauditDTO;
import cn.hsa.fqz.task.service.dto.DealResultDTO;
import cn.hsa.hsaf.core.framework.util.CurrentUser;
import cn.hsa.hsaf.core.framework.util.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hsa.fqz.task.entity.TTaskMedcClueDO;
import cn.hsa.fqz.task.service.TTaskMedcClueService;
import cn.hsa.fqz.task.dao.TTaskMedcClueMapper;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 *
 */
@Service
public class TTaskMedcClueServiceImpl extends ServiceImpl<TTaskMedcClueMapper, TTaskMedcClueDO>
    implements TTaskMedcClueService{

    @Override
    public PageResult<TTaskMedcClueDO> findListByTaskId(Integer pageNum, Integer pageSize, Integer taskId) {
        LambdaQueryWrapper<TTaskMedcClueDO>
                wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TTaskMedcClueDO::getTaskId,taskId);
        Page<TTaskMedcClueDO> page = new Page<>(pageNum,pageSize);
        IPage<TTaskMedcClueDO> pages =  this.page(page,wrapper);

        PageResult<TTaskMedcClueDO> result = new PageResult<>();
        result.setData(pages.getRecords());
        result.setPageNum(pageNum);
        result.setPageSize(pageSize);
        result.setPages((int)pages.getPages());
        result.setRecordCounts(this.count(wrapper));
        return result;
    }

    @Override
    public Boolean confirmDealType(String sequenceNo, Integer dealType) {
        LambdaQueryWrapper<TTaskMedcClueDO>
                wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TTaskMedcClueDO::getSequenceNo,sequenceNo);

        TTaskMedcClueDO clueDO = this.getOne(wrapper);
        clueDO.setDealState(DealState.FEEDBACKING.getValue());
        return this.updateById(clueDO);
    }

    @Override
    public Boolean applyDealComplt(DealResultDTO dto, CurrentUser currentUser) {

        LambdaQueryWrapper<TTaskMedcClueDO>
                wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TTaskMedcClueDO::getSequenceNo,dto.getSequenceNo());

        TTaskMedcClueDO clueDO = this.getOne(wrapper);

        clueDO.setDealState(DealState.APPLY_DEAL_COMPLT.getValue());

        clueDO.setDealApplyCplter(currentUser.getUserAcctID());
        clueDO.setDealApplyTime(new Date());

        return this.updateById(clueDO);
    }

    @Override
    public Boolean confirmDealComplt(String sequenceNo, CurrentUser currentUser) {
        LambdaQueryWrapper<TTaskMedcClueDO>
                wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TTaskMedcClueDO::getSequenceNo,sequenceNo);

        TTaskMedcClueDO clueDO = this.getOne(wrapper);

        clueDO.setDealState(DealState.COMPLT.getValue());

        clueDO.setDealConfirmCplter(currentUser.getUserAcctID());
        clueDO.setDealConfirmTime(new Date());

        return this.updateById(clueDO);
    }

    @Override
    public Boolean applyReaudit(ApplyReauditDTO dto, CurrentUser currentUser) {
        LambdaQueryWrapper<TTaskMedcClueDO>
                wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TTaskMedcClueDO::getSequenceNo,dto.getSequenceNo());

        TTaskMedcClueDO clueDO = this.getOne(wrapper);

        clueDO.setDealState(DealState.RE_ADUIT.getValue());
        clueDO.setDealReason(dto.getReason());

        clueDO.setDealConfirmCplter(currentUser.getUserAcctID());
        clueDO.setDealConfirmTime(new Date());

        return this.updateById(clueDO);
    }
}




